<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>roles/client/uiHelpers.js - The meteor-roles API</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.9.1/build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="icon" href="../assets/favicon.ico">
    <script src="http://yui.yahooapis.com/combo?3.9.1/build/yui/yui-min.js"></script>
</head>
<body class="yui3-skin-sam">

<div id="doc">
    <div id="hd" class="yui3-g header">
        <div class="yui3-u-3-4">
                <h1><img src="../assets/css/logo.png" title="The meteor-roles API" width="117" height="52"></h1>
        </div>
        <div class="yui3-u-1-4 version">
            <em>API Docs for: v1.2.14</em>
        </div>
    </div>
    <div id="bd" class="yui3-g">

        <div class="yui3-u-1-4">
            <div id="docs-sidebar" class="sidebar apidocs">
                <div id="api-list">
                    <h2 class="off-left">APIs</h2>
                    <div id="api-tabview" class="tabview">
                        <ul class="tabs">
                            <li><a href="#api-classes">Classes</a></li>
                            <li><a href="#api-modules">Modules</a></li>
                        </ul>
                
                        <div id="api-tabview-filter">
                            <input type="search" id="api-filter" placeholder="Type to filter APIs">
                        </div>
                
                        <div id="api-tabview-panel">
                            <ul id="api-classes" class="apis classes">
                                <li><a href="../classes/Roles.html">Roles</a></li>
                                <li><a href="../classes/UIHelpers.html">UIHelpers</a></li>
                            </ul>
                
                
                            <ul id="api-modules" class="apis modules">
                                <li><a href="../modules/Roles.html">Roles</a></li>
                                <li><a href="../modules/UIHelpers.html">UIHelpers</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="yui3-u-3-4">
                <div id="api-options">
                    Show:
                    <label for="api-show-inherited">
                        <input type="checkbox" id="api-show-inherited" checked>
                        Inherited
                    </label>
            
                    <label for="api-show-protected">
                        <input type="checkbox" id="api-show-protected">
                        Protected
                    </label>
            
                    <label for="api-show-private">
                        <input type="checkbox" id="api-show-private">
                        Private
                    </label>
                    <label for="api-show-deprecated">
                        <input type="checkbox" id="api-show-deprecated">
                        Deprecated
                    </label>
            
                </div>
            
            <div class="apidocs">
                <div id="docs-main">
                    <div class="content">
<h1 class="file-heading">File: roles/client/uiHelpers.js</h1>

<div class="file">
    <pre class="code prettyprint linenums">
&quot;use strict&quot;

/**
 * Convenience functions for use on client.
 *
 * NOTE: You must restrict user actions on the server-side; any
 * client-side checks are strictly for convenience and must not be
 * trusted.
 *
 * @module UIHelpers
 */


////////////////////////////////////////////////////////////
// UI helpers
//
// Use a semi-private variable rather than declaring UI
// helpers directly so that we can unit test the helpers.
// XXX For some reason, the UI helpers are not registered 
// before the tests run.
//
Roles._uiHelpers = {

  /**
   * UI helper to check if current user is in at least one
   * of the target roles.  For use in client-side templates.
   *
   * @example
   *     {{#if isInRole &#x27;admin&#x27;}}
   *     {{/if}}
   *
   *     {{#if isInRole &#x27;editor,user&#x27;}}
   *     {{/if}}
   *
   *     {{#if isInRole &#x27;editor,user&#x27; &#x27;scope1&#x27;}}
   *     {{/if}}
   *
   * @method isInRole
   * @param {String} role Name of role or comma-seperated list of roles.
   * @param {String} [scope] Optional, name of scope to check.
   * @return {Boolean} &#x60;true&#x60; if current user is in at least one of the target roles.
   * @static
   * @for UIHelpers 
   */
  isInRole: function (role, scope) {
    var user = Meteor.user(),
        comma = (role || &#x27;&#x27;).indexOf(&#x27;,&#x27;),
        roles

    if (!user) return false
    if (!Match.test(role, String)) return false

    if (comma !== -1) {
      roles = _.reduce(role.split(&#x27;,&#x27;), function (memo, r) {
        if (!r || !Roles._trim(r)) {
          return memo
        }
        memo.push(Roles._trim(r))
        return memo
      }, [])
    } else {
      roles = [role]
    }

    if (Match.test(scope, String)) {
      return Roles.userIsInRole(user, roles, scope)
    }

    return Roles.userIsInRole(user, roles)
  }
}



////////////////////////////////////////////////////////////
// Register UI helpers
//

if (Roles.debug &amp;&amp; console.log) {
  console.log(&quot;[roles] Roles.debug =&quot;, Roles.debug)
}

if (&#x27;undefined&#x27; !== typeof Package.blaze &amp;&amp;
    &#x27;undefined&#x27; !== typeof Package.blaze.Blaze &amp;&amp;
    &#x27;function&#x27;  === typeof Package.blaze.Blaze.registerHelper) {
  _.each(Roles._uiHelpers, function (func, name) {
    if (Roles.debug &amp;&amp; console.log) {
      console.log(&quot;[roles] registering Blaze helper &#x27;&quot; + name + &quot;&#x27;&quot;)
    }
    Package.blaze.Blaze.registerHelper(name, func) 
  })
}

    </pre>
</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script src="../assets/js/yui-prettify.js"></script>
<script src="../assets/../api.js"></script>
<script src="../assets/js/api-filter.js"></script>
<script src="../assets/js/api-list.js"></script>
<script src="../assets/js/api-search.js"></script>
<script src="../assets/js/apidocs.js"></script>
</body>
</html>
